package com.zsix.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.zsix.common.common.page.BaseController;
import com.zsix.common.common.page.PageDataTable;
import com.zsix.common.entity.formComponents.FormEntity;
import com.zsix.common.entity.formComponents.FormMenuEntity;
import com.zsix.common.entity.formComponents.FormSortEntity;
import com.zsix.common.util.ResultRes;
import com.zsix.framework.aspectj.annotation.OperLog;
import com.zsix.framework.aspectj.operate.LogConst;
import com.zsix.system.service.IFormMapService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/form")
public class FormMapController extends BaseController {
    @Autowired
    private IFormMapService formMapService;

    /**
     * 表单列表
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取表单列表")
    @PostMapping("/formList")
    public PageDataTable formList(@RequestBody FormEntity formEntity){
        startPage();
        List<FormEntity> list=formMapService.formList(formEntity);
        return getDataTable(list);
    }
    /**
     * 获取表单应用列表
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取表单应用列表")
    @PostMapping("/formApplicationList")
    public PageDataTable formApplicationList(@RequestBody FormMenuEntity param){
        startPage();
        List<FormMenuEntity> list=formMapService.formApplicationList(param);
        return getDataTable(list);
    }
    /**
     * 发布表单菜单
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "发布表单菜单")
    @PostMapping("/setFormMenu")
    public ResultRes setFormMenu(@RequestBody FormMenuEntity param){
        return ResultRes.success(formMapService.setFormMenu(param));
    }
    /**
     * 获取表单菜单信息
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取表单菜单信息")
    @GetMapping("/getFormAppSetting")
    public ResultRes getFormAppSetting(HttpServletResponse response,
                                       @RequestParam("id") String id){
        return ResultRes.success(formMapService.getFormAppSetting(id));
    }
    /**
     * 删除表单应用
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "删除表单应用")
    @PostMapping("/depeteFormMenu")
    public ResultRes depeteFormMenu(@RequestBody FormMenuEntity param){
        return ResultRes.success(formMapService.depeteFormMenu(param));
    }
    /**
     * 获取表单树
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取表单树")
    @GetMapping("/getFormTree")
    public ResultRes getFormTree(){
        return ResultRes.success(formMapService.getFormTree());
    }
    /**
     * 获取表单树，增加时使用
     * @return
     */
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取表单树")
    @GetMapping("/getFormTreeByAdd")
    public ResultRes getFormTreeByAdd(){
        return ResultRes.success(formMapService.getFormTreeByAdd());
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取/查看单个表单")
    @GetMapping("/getFormEntity")
    public ResultRes getFormEntity(HttpServletResponse response,
                                   @RequestParam("id") String id){
        FormEntity formEntity=formMapService.getFormEntity(id);
        if(formEntity!=null) {
            return ResultRes.success(formEntity);
        }
        else{
            return ResultRes.error("不存在id为["+id+"]的对象。");
        }
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.DELETE , operDesc = "修改/创建")
    @PostMapping("/editForm")
    public ResultRes editForm(@RequestBody JSONObject param) {
        return success(formMapService.editForm(param));
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.DELETE , operDesc = "删除表单")
    @PostMapping("/deleteForm")
    public ResultRes deleteForm(@RequestBody JSONObject param){
        String msg=formMapService.deleteForm(param.getString("id"));
        if(msg.equals("执行成功")) {
            return ResultRes.success(msg);
        }
        else{
            return ResultRes.error(msg);
        }
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取表单分类列表")
    @GetMapping("/formSortList")
    public ResultRes formSortList(){
        return ResultRes.success(formMapService.formSortList());
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "查看某个表单分类信息")
    @GetMapping("/getFormSort")
    public ResultRes getFormSort(HttpServletResponse response,
                                 @RequestParam("id") String id){
        FormSortEntity formSortEntity=formMapService.getFormSort(id);
        if(formSortEntity!=null) {
            return ResultRes.success(formSortEntity);
        }
        else{
            return ResultRes.error("不存在id为["+id+"]的对象。");
        }
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.INSERT , operDesc = "增加表单分类")
    @PostMapping("/addFormSort")
    public ResultRes addFormSort(@RequestBody JSONObject param){
        String id=UUID.randomUUID().toString();
        String name=param.getString("name");
        String parentNo=param.getString("parentNo");
        String msg=formMapService.addFormSort(id,name,parentNo);
        if(msg.equals("执行成功")) {
            return ResultRes.success(msg);
        }
        else{
            return ResultRes.error(msg);
        }
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.DELETE , operDesc = "删除表单分类")
    @PostMapping("/deleteFormSort")
    public ResultRes deleteFormSort(@RequestBody JSONObject param){
        String id=param.getString("id");
        String msg=formMapService.deleteFormSort(id);
        if(msg.equals("执行成功")) {
            return ResultRes.success(msg);
        }
        else{
            return ResultRes.error(msg);
        }
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.UPDATE , operDesc = "更新表单分类")
    @PostMapping("/updateFormSort")
    public ResultRes updateFormSort(@RequestBody JSONObject param){
        String id=param.getString("id");
        String name=param.getString("name");
        String parentNo=param.getString("parentNo");
        String msg=formMapService.updateFormSort(id,name,parentNo);
        if(msg.equals("执行成功")) {
            return ResultRes.success(msg);
        }
        else{
            return ResultRes.error(msg);
        }
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.OTHER , operDesc = "获取表单数据")
    @GetMapping("/getGenerWork")
    public ResultRes getGenerWork(HttpServletResponse response,
                                  @RequestParam("id") String id){
        try{
            return ResultRes.success(formMapService.getGenerWork(id));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }

    /**
     * delete
     * @param response
     * @param linkId
     * @return
     */
    @GetMapping("/getGenerWorkByLink")
    public ResultRes getGenerWorkByLink(HttpServletResponse response,
                                  @RequestParam("linkId") String linkId,
                                        @RequestParam("formKey") String formKey){
        try{
            return ResultRes.success(formMapService.getGenerWorkByLink(linkId,formKey));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
    @GetMapping("/getGenerWorkByFKId")
    public ResultRes getGenerWorkByFKId(HttpServletResponse response,
                                        @RequestParam("fk_id") String fk_id,
                                        @RequestParam("formKey") String formKey){
        try{
            return ResultRes.success(formMapService.getGenerWorkByFKId(fk_id,formKey));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
    @OperLog(operModul = "表单操作" , operType = LogConst.DELETE , operDesc = "删除表单数据")
    @PostMapping("/deleteGenerWork")
    public ResultRes deleteGenerWork(@RequestBody JSONObject param){
        String id=param.getString("frmId");
        String list=param.getString("list");
        String msg=formMapService.deleteGenerWork(id,list);
        if(msg.equals("suess")){
            return ResultRes.success("删除成功。");
        }
        else{
            return ResultRes.error(msg);
        }
    }
}
